package eq.util;

public class MyMath {

	/**
	 * compute the greatest common divisor of integer a and b;
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static long gcd(long a, long b) {
		return (b != 0) ? gcd(b, a % b) : a;
	}

	public static int gcd(int a, int b) {
		return (b != 0) ? gcd(b, a % b) : a;
	}

	public static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	public static int lcm(int a, int b) {
		return a * b / gcd(a, b);
	}

	public static boolean isSysmetric(int n) {
		char cs[] = Integer.toString(n).toCharArray();
		for (int i = 0; i < cs.length / 2; i++)
			if (cs[i] != cs[cs.length - 1 - i])
				return false;
		return true;
	}

	public static int getReverse(int n) {
		char cs[] = Integer.toString(n).toCharArray();
		for (int i = 0; i < cs.length / 2; i++) {
			char t = cs[i];
			cs[i] = cs[cs.length - 1 - i];
			cs[cs.length - 1 - i] = t;
		}
		return Integer.parseInt(new String(cs));
	}

	public static boolean isSquare(int n) {
		// TODO
		int r = (int) (Math.sqrt(n) + 0.5);
		return r * r == n;
	}

	public static boolean isPrime(int n) {
		if (n == 1)
			return false;
		for (int c = 2; c * c <= n; c++)
			if (n % c == 0)
				return false;
		return true;
	}

}
